﻿using OA.BLL;

using OA.Model;
using OA.Web.Areas.Member.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using OA.Core;
using System.Linq.Expressions;
using Microsoft.Extensions.Caching.Memory;
using OA.Web.Code;
using Microsoft.AspNetCore.Http;
using OA.Web.Attribute;
using Microsoft.AspNetCore.Mvc;
using OA.Core.EF;
using X.PagedList;

namespace OA.Web.Areas.Member.Controllers
{
    public class KnowledgeController : BaseMemberController
    {

        private IMemoryCache _cache;
        private ICookie _cookie;
        public KnowledgeController(ICookie cookie, IHttpContextAccessor accessor, IMemoryCache cache) : base(accessor, cookie, cache)
        {
            _cache = cache;
            _cookie = cookie;
        }

        [MyActionFilter("203004", "知识宝库", "通知公告", true, "iconzhishi")]
        public IActionResult Index()
        {
            List<SimpleKnowledgeGroup> groups = this.GetAll<KnowledgeGroup, SimpleKnowledgeGroup>(m => m.Status != -1, SimpleKnowledgeGroup.Map);
            ViewBag.KnowledgeGroup = groups;
            return View();
        }

        [MyActionFilter("203004", "知识宝库")]
        public IActionResult GetData(string keyword, int typeID = 0, int page = 1, int limit = 20)
        {
            ViewBag.TypeID = typeID;
            Expression<Func<VKnowledge, bool>> where = m => true;
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                where = where.AndAlso<VKnowledge>(m => m.Title.Contains(keyword));
            }
            if (typeID > 0)
            {
                where = where.AndAlso<VKnowledge>(m => m.GroupID == typeID);
            }
            var list = this.dbContextFactory.DbContext.VKnowledge.Where(where).OrderByDescending(m => m.ID).ToPagedList(page, limit);
            var result = new
            {
                code = "0",
                msg = "",
                count = list.TotalItemCount,
                data = list
            };
            return Json(result);
        }


        [HttpGet]
        [MyActionFilter(key: "203004", name: "知识查看")]
        public IActionResult Details(int id)
        {
            Knowledge infoKnowledge = this.dbContextFactory.DbContext.Knowledge.Where(m => m.ID == id).FirstOrDefault();
            if (infoKnowledge == null)
            {
                return Redirect("/Member/Tips/Index?msg=您查看的知识点不存在");
            }
            ViewBag.KnowledgeGroup = this.dbContextFactory.DbContext.KnowledgeGroup.Where(m => m.ID == infoKnowledge.GroupID).FirstOrDefault();
            
            return View(infoKnowledge);
        }

    }
}
